package cn.wolfcode.crm.web.controller;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.List;


@Controller
@RequestMapping("role")
public class RoleController {
    @Autowired
    private IRoleService service;

    @RequestMapping("")
    public String index(){
        return "role/list";
    }

    @RequestMapping("list")
    @ResponseBody
    @RequiresPermissions("role:data")
    @PermissionName("角色数据")
    public PageResult list(QueryObject qo){
        return service.query(qo);
    }


    @RequestMapping("queryRoles")
    @ResponseBody
    public List<Role> queryRoles(){
        return service.list();
    }

    //根据员工id查询所有角色id
    @RequestMapping("queryRoleIdByEmpId")
    @ResponseBody
    public List<Long> queryRoleIdByEmpId(Long empId){
        return service.queryRoleIdByEmpId(empId);
    }


    @RequestMapping("save")
    @ResponseBody
    @RequiresPermissions("role:save")
    @PermissionName("角色保存")
    public JsonResult save(Role role){
        JsonResult result = new JsonResult();
        try {
            service.save(role);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult("保存失败");
        }
        return result;
    }

    @RequestMapping("update")
    @ResponseBody
    @RequiresPermissions("role:update")
    @PermissionName("角色更新")
    public JsonResult update(Role role){
        JsonResult result = new JsonResult();
        try {
            service.update(role);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult("编辑失败");
        }
        return result;
    }

    @RequestMapping("delete")
    @ResponseBody
    @RequiresPermissions("role:delete")
    @PermissionName("角色删除")
    public JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try {
            service.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult("操作失败");
        }
        return result;
    }

}
